<?php


namespace Blog\App\Action;

use Hx\App;
use Hx\Event;
use Hx\Response;
use Hx\View;

/**
 * 主要Action操作类
 * Class Index
 * @package Blog\App\Action
 */
class Index {

    /**
     * 显示首页
     * @param int $page 页数
     * @return string
     */
    public function main($page = 1) {
        $page = (int)$page;
        $viewData = array();
        if ($page > 1) {
            $viewData['title'] = '第 ' . $page . ' 页';
        }
        $viewData['sideHtml'] = Event::trigger('side-html');
        $viewData['blogData'] = App::Model('Blog->getPaged', array('1 = 1', $page, 4));
        $viewData['pageUrl'] = '/page/';
        return View::display('index.html', 2, $viewData);
    }

    /**
     * 显示作者下的文章
     * @param int $userId 作者id
     * @param int $page 页数
     * @return string
     */
    public function user($userId, $page = 1) {
        $user = App::Model('User->getOne', array('id', $userId));
        if (!$user) {
            Response::sendRedirect('/error/404.html');
        }
        $viewData = array();
        $page = (int)$page;
        $viewData['sideHtml'] = Event::trigger('side-html');
        $viewData['pageUrl'] = '/author/' . $userId . '/';
        $viewData['userData'] = $user;
        $viewData['blogData'] = App::Model('Blog->getPaged', array('userId = ' . $userId, $page, 4));
        $viewData['title'] = $user->nickName . ($page > 1 ? ' - 第 ' . $page . ' 页' : '');
        return View::display('index.html', 2, $viewData);
    }

    /**
     * 分类下文章列表
     * @param string $categoryKey 分类关键字
     * @param int $page
     * @return string
     */
    public function category($categoryKey, $page = 1) {
        $category = App::Model('Category->getOne', array('keyString', $categoryKey));
        if (!$category) {
            Response::sendRedirect('/error/404.html');
        }
        $viewData = array();
        $page = (int)$page;
        $viewData['sideHtml'] = Event::trigger('side-html');
        $viewData['pageUrl'] = '/category/' . $categoryKey . '/';
        $viewData['categoryData'] = $category;
        $viewData['blogData'] = App::Model('Blog->getPaged', array('categoryId = "' . $category->id . '"', $page, 4));
        $viewData['title'] = $category->name . ($page > 1 ? ' - 第 ' . $page . ' 页' : '');
        Event::trigger('category-update');
        return View::display('index.html', 2, $viewData);
    }

    /**显示归档
     * @return string
     */
    public function all() {
        $viewData = array();
        $viewData['blogData'] = App::Model('Blog->getAll', array(true));
        $viewData['title'] = '归档';
        return View::display('all.html', 2, $viewData);
    }

}